
// color util
let rgb2hsl = function (r, g, b) {
	r /= 255, g /= 255, b /= 255;
	var max = Math.max(r, g, b),
		min = Math.min(r, g, b);
	var h, s, l = (max + min) / 2;

	if (max == min) {
		h = s = 0; // achromatic
	} else {
		var d = max - min;
		s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
		switch (max) {
			case r:
				h = (g - b) / d + (g < b ? 6 : 0);
				break;
			case g:
				h = (b - r) / d + 2;
				break;
			case b:
				h = (r - g) / d + 4;
				break;
		}
		h /= 6;
	}
	return [h, s, l];
}

let rgbColor2Hsl = function (
	rgbColor) {
	const ca = rgbColorArrayWithRgbColor(rgbColor);
	if (ca.length !== 3) {
		return;
	}
	let r = Number(ca[0]);
	let g = Number(ca[1]);
	let b = Number(ca[2]);
	r /= 255, g /= 255, b /= 255;
	var max = Math.max(r, g, b),
		min = Math.min(r, g, b);
	var h, s, l = (max + min) / 2;

	if (max == min) {
		h = s = 0; // achromatic
	} else {
		var d = max - min;
		s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
		switch (max) {
			case r:
				h = (g - b) / d + (g < b ? 6 : 0);
				break;
			case g:
				h = (b - r) / d + 2;
				break;
			case b:
				h = (r - g) / d + 4;
				break;
		}
		h /= 6;
	}
	return [h, s, l];
}

let hslToRgb = function (h, s, l) {
	var r, g, b;
	if (s == 0) {
		r = g = b = l; // achromatic
	} else {
		var hue2rgb = function hue2rgb(p, q, t) {
			if (t < 0) t += 1;
			if (t > 1) t -= 1;
			if (t < 1 / 6) return p + (q - p) * 6 * t;
			if (t < 1 / 2) return q;
			if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
			return p;
		}
		var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
		var p = 2 * l - q;
		r = hue2rgb(p, q, h + 1 / 3);
		g = hue2rgb(p, q, h);
		b = hue2rgb(p, q, h - 1 / 3);
	}
	return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}

function HSVtoRGB(h, s, v) {
  let i, f, p1, p2, p3;
  let r = 0, g = 0, b = 0;
  if (s < 0) s = 0;
  if (s > 1) s = 1;
  if (v < 0) v = 0;
  if (v > 1) v = 1;
  h %= 360;
  if (h < 0) h += 360;
  h /= 60;
  i = Math.floor(h);
  f = h - i;
  p1 = v * (1 - s);
  p2 = v * (1 - s * f);
  p3 = v * (1 - s * (1 - f));
  switch(i) {
      case 0: r = v;  g = p3; b = p1; break;
      case 1: r = p2; g = v;  b = p1; break;
      case 2: r = p1; g = v;  b = p3; break;
      case 3: r = p1; g = p2; b = v;  break;
      case 4: r = p3; g = p1; b = v;  break;
      case 5: r = v;  g = p1; b = p2; break;
  }
  return 'rgb(' + Math.round(r * 255) + ',' + Math.round(g * 255) + ',' + Math.round(b * 255) + ')';
}

var colorHex = function (color) {
	var that = color;
	//十六进制颜色值的正则表达式
	var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
	// 如果是rgb颜色表示
	if (/^(rgb|RGB)/.test(that)) {
		var aColor = that.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(",");
		var strHex = "#";
		for (var i = 0; i < aColor.length; i++) {
			var hex = Number(aColor[i]).toString(16);
			if (hex.length < 2) {
				hex = '0' + hex;
			}
			strHex += hex;
		}
		if (strHex.length !== 7) {
			strHex = that;
		}
		return strHex;
	} else if (reg.test(that)) {
		var aNum = that.replace(/#/, "").split("");
		if (aNum.length === 6) {
			return that;
		} else if (aNum.length === 3) {
			var numHex = "#";
			for (var i = 0; i < aNum.length; i += 1) {
				numHex += (aNum[i] + aNum[i]);
			}
			return numHex;
		}
	}
	return that;
}

var colorRgb = function (hexColor) {
  // 16进制颜色值的正则
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  // 把颜色值变成小写
  var color = hexColor.toLowerCase();
  if (reg.test(color)) {
    // 如果只有三位的值，需变成六位，如：#fff => #ffffff
    if (color.length === 4) {
      var colorNew = "#";
      for (var i = 1; i < 4; i += 1) {
        colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
      }
      color = colorNew;
    }
    // 处理六位的颜色值，转为RGB
    var colorChange = [];
    for (var i = 1; i < 7; i += 2) {
      colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
    }
    return "RGB(" + colorChange.join(",") + ")";
  } else {
    return color;
  }
}

var rgbColorArrayWithRgbColor = function(rgbColor) {
	// 如果是rgb颜色表示
	if (/^(rgb|RGB)/.test(rgbColor)) {
		var aColor = rgbColor.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(",");
		return aColor;
	}
	return rgbColor;
}

function getHue(rgbArray) {
  let r, g, b, max, min;
  r = parseInt(rgbArray[0]);
  g = parseInt(rgbArray[1]);
  b = parseInt(rgbArray[2]);
  max = Math.max(r, g, b)
  min = Math.min(r, g, b)
  if(max == min) {
      return 0;
  } else {
      if( max == r && g >= b) {
          return 60 * (g - b)/(max - min);
      } else if ( max == r && g < b) {
          return 60 * (g - b)/(max - min) + 360;
      } else if (max == g) {
          return 60 * (b - r)/(max - min) + 120;
      } else if (max == b) {
          return 60 * (r - g)/(max - min) + 240;
      }  
  }
}

module.exports = {
  rgb2hsl: rgb2hsl,
	rgbColor2Hsl: rgbColor2Hsl,
  hslToRgb: hslToRgb,
  HSVtoRGB: HSVtoRGB,
	colorHex: colorHex,
  colorRgb: colorRgb,
  getHue: getHue,
  rgbColorArrayWithRgbColor: rgbColorArrayWithRgbColor
}